# -*- makefile -*-

# Adapted from Makefile.msp430
# Adapted from Makefile.at91sam7

### Define the CPU directory
CONTIKI_CPU=$(CONTIKI)/cpu/mc1322x

### Define the source files we have in the AT91SAM7S port

CONTIKI_CPU_DIRS = . lib src board dev ../arm/common/dbg-io

MC1322X = debug-uart.c rtimer-arch.c watchdog.c contiki-maca.c contiki-misc.c leds-arch.c leds.c contiki-uart.c slip-uart1.c init.c config.c

DBG_IO = dbg-printf.c dbg-snprintf.c dbg-sprintf.c strformat.c

CPU_LIBS = $(notdir $(wildcard $(CONTIKI_CPU)/lib/*.c))
CPU_SRCS = $(notdir $(wildcard $(CONTIKI_CPU)/src/*.c)) $(notdir $(wildcard $(CONTIKI_CPU)/src/*.S))

CONTIKI_TARGET_SOURCEFILES += $(MC1322X) $(DBG_IO) $(CPU_LIBS) $(CPU_SRCS) $(SYSAPPS) $(ELFLOADER) \
			$(TARGETLIBS) $(UIPDRIVERS) $(USB)

CONTIKI_SOURCEFILES        += $(CONTIKI_TARGET_SOURCEFILES)

THREADS =

CROSS_COMPILE ?= arm-none-eabi-
CROSS ?= $(CROSS_COMPILE)

### Compiler definitions
CC       = $(CROSS)gcc
LD       = $(CROSS)gcc
AS       = $(CROSS)as
AR       = $(CROSS)ar
NM       = $(CROSS)nm
OBJCOPY  = $(CROSS)objcopy
OBJDUMP  = $(CROSS)objdump
STRIP    = $(CROSS)strip

PROJECT_OBJECTFILES += ${addprefix $(OBJECTDIR)/,$(CONTIKI_TARGET_MAIN:.c=.o)}

TEXT_BASE = 0x00400000
export TEXT_BASE
LINKERSCRIPT = $(OBJECTDIR)/mc1322x.lds

STARTUP=$(OBJECTDIR)/start.o

ARCH_FLAGS= -mcpu=arm7tdmi-s -mthumb-interwork -march=armv4t -mtune=arm7tdmi-s -DCONFIG_ARM -D__ARM__ #-Wcast-align
THUMB_FLAGS= -mthumb -mcallee-super-interworking
ARM_FLAGS= -marm

CFLAGSNO = -I. -I$(CONTIKI)/core -I$(CONTIKI_CPU) -I$(CONTIKI_CPU)/loader \
	   -I$(CONTIKI_CPU)/dbg-io \
           -I$(CONTIKI)/platform/$(TARGET) \
	   -I$(CONTIKI_CPU)/lib/include \
	   -I$(CONTIKI_CPU)/src \
	   -I$(CONTIKI_CPU)/board \
           ${addprefix -I,$(APPDIRS)} \
           -DWITH_ASCII -DMCK=$(MCK) \
           -Werror $(ARCH_FLAGS) -g 

CFLAGS  += $(CFLAGSNO) -Os -DRUN_AS_SYSTEM -DROM_RUN -fno-strict-aliasing  -fno-common -ffixed-r8 -msoft-float -DTEXT_BASE=$(TEXT_BASE) -fno-builtin-printf -fno-builtin-sprintf -ffunction-sections -fdata-sections -MMD 
LDFLAGS += -T $(LINKERSCRIPT) -nostartfiles -static -u_start -Wl,-Map=contiki-$(TARGET).map,-export-dynamic,--gc-sections
AFLAGS := $(AFLAGS_DEBUG) -D__ASSEMBLY__ $(CPPFLAGS) -gstabs

CDEPFLAGS = $(CFLAGS) -D __MAKING_DEPS__

### Setup directory search path for source files

CUSTOM_RULE_C_TO_OBJECTDIR_O=yes
CUSTOM_RULE_C_TO_O=yes

CFLAGS += -I$(OBJECTDIR) -I$(CONTIKI_CPU)/board -DBOARD=$(TARGET)
CPPFLAGS += -P -C ${addprefix -D,${subst $(COMMA), ,$(DEFINES)}}


$(OBJECTDIR)/board.h: $(CONTIKI_CPU)/board/board.h | $(OBJECTDIR)
ifeq ($(HOST_OS),Windows)
	ln -f $< $@
else
	ln -sf ../$< $@
endif

$(OBJECTDIR)/%.lds: $(CONTIKI_CPU)/%.lds.S | $(OBJECTDIR)
	$(TRACE_CC)
	$(Q)$(CPP) $(CPPFLAGS) $< > $@

$(OBJECTDIR)/isr.o: $(CONTIKI_CPU)/src/isr.c | $(OBJECTDIR)
	$(TRACE_CC)
	$(Q)$(CC) $(CFLAGS) $(ARM_FLAGS) $< -c -o $@

%.o: %.c
	$(TRACE_CC)
	$(Q)$(CC) $(CFLAGS) $(THUMB_FLAGS) $< -c

$(OBJECTDIR)/%.o: %.c | $(OBJECTDIR)
	$(TRACE_CC)
	$(Q)$(CC) $(CFLAGS) $(THUMB_FLAGS) -c $< -o $@

CUSTOM_RULE_S_TO_OBJECTDIR_O = yes

$(OBJECTDIR)/%.o: %.S | $(OBJECTDIR)
	$(TRACE_CC)
	$(Q)$(CC) $(CFLAGS) $(AFLAGS) $(ARM_FLAGS) $< -c  -o $@

CUSTOM_RULE_C_TO_CO=yes

%.co: %.c
	$(TRACE_CC)
	$(Q)$(CC) $(CFLAGS) -DAUTOSTART_ENABLE $(THUMB_FLAGS) $< -c -o $@

.PRECIOUS: %.bin %_$(TARGET).bin

%.ihex: %.$(TARGET)
	$(OBJCOPY) $^ -O ihex $@

%_$(TARGET).bin: %.elf
	$(OBJCOPY) -O binary $< $@

%.$(TARGET): %_$(TARGET).bin
	@

%.elf: $(OBJECTDIR)/board.h  %.co $(PROJECT_OBJECTFILES) contiki-$(TARGET).a $(STARTUP) $(OBJECTDIR)/mc1322x.lds
	$(TRACE_CC)
	$(Q)$(CC) $(LDFLAGS) $(CFLAGS) -nostartfiles -o $@ $(filter-out %.a %.lds,$^) $(filter %.a,$^) $(filter %.a,$^)

%.lst: %.elf
	$(OBJDUMP) -DS $< > $@

